<!DOCTYPE html>
<html>
<head>
    <!-- 答辩代码 别看了呃呃 抢票用的是微软表格有定时 不用找了 -->
    <meta charset="UTF-8" />
    <title>抢票倒计时</title>
    <style>

        #countdown {
            font-size: 24px;
            font-weight: bold;
            text-align: center;
            margin-top: 20px;
            text-shadow: 0 0 10px rgba(36, 75, 84, 0.9);
        }

        #buy-ticket {
            display: block;
            width: 240px;
            height: 60px;
            margin: 20px auto;
            background-color: green;
            color: white;
            text-align: center;
            line-height: 60px;
            text-decoration: none;
            font-size: 18px;
            font-weight: bold;
            border-radius: 5px;
            transition: all 0.2s ease;

            box-shadow: 0 0 10px rgba(36, 75, 84, 0.9);
        }

        #buy-ticket:hover {
            background-color: darkgreen;
            box-shadow: 0 0 20px rgba(36, 75, 84, 0.9);
        }

        #form-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.5);
            align-items: center;
            justify-content: center;
        }

        #form-modal .content {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }
        body {
            display: flex;

            justify-content: center;
            align-items: center;
            justify-items: center;

            color:white;

            background-image: url("img/bg.jpg");
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }

        #background_video{
            position: fixed;
            top: 50%;
            left: 50%;
            width: 100%;
            height: 100%;
            z-index: -1;

            -webkit-transform: translate(-50%, -50%);
            -moz-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
        }

        #main-wrap{
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            width: 100%;
            height: 95vh;

        }

        #footer{
            /* margin-bottom: 10px; */
            left: 0;
            right: 0;
            text-align: center;
            /* line-height: 5vh; */
            text-shadow: 0 0 5px rgba(36, 75, 84, 0.9);
        }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
</head>

<body>
    <img id="background_video" src="img/bg.jpg" style="object-fit: cover; object-position: center;" />
    <div id="main-wrap">
        <img src="img/logo.png" alt="logo" style="width: 100%; max-width: 640px; margin: 40px auto; display: block;" />
        <div id="countdown">Loading...</div>
        <div id="buy-ticket">立即抢票</a>
            <div id="form-modal">
                <div class="content">
                    <iframe id="form-iframe" width="640px" height="480px"
                        src="https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpURVM1TTBRUFVJRFBZV1dNUVdXNE5TU1ZUNy4u&embed=true"
                        frameborder="0" marginwidth="0" marginheight="0"
                        style="border: none; max-width:100%; max-height:100vh" allowfullscreen webkitallowfullscreen
                        mozallowfullscreen msallowfullscreen> </iframe>
                </div>
            </div>
        </div>
        <div id="footer">
            <h5>技术支持 / 橙橙<br>图片设计 / 小解<br>后台管理 / 计算机学生会策联部<br>审核 / 计算机学生会主席团</h5>
        </div>
    </div>
    <script>
        const countdownElement = document.getElementById("countdown");
        const buyTicketButton = document.getElementById("buy-ticket");
        const formIframe = document.getElementById("form-iframe");
        let displayed = false;
    
        // Define events data
        const eventsData = [
            {
                start: "2023-05-5 03:00:00",
                end: "2023-05-8 0:33:59",
                link: "https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpURVM1TTBRUFVJRFBZV1dNUVdXNE5TU1ZUNy4u&embed=true"
            },
            {
                start: "2023-05-12 11:00:00",
                end: "2023-05-12 12:30:00",
                link: "https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpUN09VR1czT0JUWTZNT09NQ0dIQTZOQ0xPNC4u&embed=true"
            },
            {
                start: "2023-05-13 11:00:00",
                end: "2023-05-13 12:30:00",
                link: "hhttps://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpUQklUT0hZUFM2MjhMSTdGMlNLQ05JUE02Sy4u"
            },
            {
                start: "2023-05-14 11:00:00",
                end: "2023-05-14 12:30:00",
                link: "https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpUQ1hFRThJUUg5MVRROVJQSUZDUVpLVklKSy4u"
            },
            {
                start: "2023-05-15 11:00:00",
                end: "2023-05-15 12:30:00",
                link: "https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpUMUVBRExEODc2QVFIQUUzT0Y0WTZCNExKVS4u"
            },
            {
                start: "2023-05-16 11:00:00",
                end: "2023-05-16 12:30:00",
                link: "https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpUNzhLWVdRSFJETFZETlBaMExIWkU4MTFEQS4u"
            },
            {
                start: "2023-05-17 11:00:00",
                end: "2023-05-17 12:30:00",
                link: "https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpUMUgyVDdBODhGVVBaTUY4RUc0OTdENFczMC4u"
            },
            {
                start: "2023-05-18 11:00:00",
                end: "2023-05-18 12:30:00",
                link: "https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAN__ial3hpURVFYMlFUNDBBR1NNQ0JJNzBHSE1UVERaSC4u"
            }
        ];

        let endTime;

const now = moment();
let currentEvent;

for (const event of eventsData) {
    const start = moment(event.start);
    const end = moment(event.end);

    if (now.isBetween(start, end)) {
        currentEvent = event;
        break;
    }
}

if (currentEvent) {
    endTime = moment(currentEvent.end).valueOf(); // Get the UNIX timestamp
} else {
    endTime = undefined;
}

function updateCountdown() {
    const now = moment();
    let currentEvent;

    for (const event of eventsData) {
        const start = moment(event.start);
        const end = moment(event.end);

        if (now.isBetween(start, end)) {
            currentEvent = event;
            break;
        }
    }

    if (!currentEvent){
        for (const event of eventsData) {
            const start = moment(event.start);
            const end = moment(event.end);
    
            if (start.isSame(now, 'day')) {
                currentEvent = event;
                break;
            }
        }
    }

    if (!currentEvent) {
        countdownElement.innerHTML = "暂无抢票活动";
        buyTicketButton.disabled = true;
        buyTicketButton.style.backgroundColor = "gray";
        return;
    }

    const start = moment(currentEvent.start);
    const end = moment(currentEvent.end);
    const remainingTime = end.diff(now);

    if (now.isBefore(start)) {
        const duration = moment.duration(start.diff(now));
        const days = duration.days();
        const hours = duration.hours();
        const minutes = duration.minutes();
        const seconds = duration.seconds();

        countdownElement.innerHTML = `距离抢票开始还有</br> ${days}天${hours.toString().padStart(2, '0')}小时${minutes.toString().padStart(2, '0')}分钟${seconds.toString().padStart(2, '0')}秒`;
        buyTicketButton.style.backgroundColor = "gray";
        buyTicketButton.disabled = true;
        displayed = false;
    } else if (now.isBetween(start, end)) {
        // currentEvent = event;
        buyTicketButton.style.backgroundColor = "green";
        buyTicketButton.disabled = false;
        const duration = moment.duration(remainingTime);
        const days = duration.days();
        const hours = duration.hours();
        const minutes = duration.minutes();
        const seconds = duration.seconds();
        countdownElement.innerHTML = `抢票进行中！</br> ${hours.toString().padStart(2, '0')}小时${minutes.toString().padStart(2, '0')}分钟${seconds.toString().padStart(2, '0')}秒`;

        // set src
        if (displayed) {
            return;
        }
        formIframe.src = currentEvent.link;
        displayed = true;
    } else {
        const duration = moment.duration(remainingTime);
        const days = duration.days();
        const hours = duration.hours();
        const minutes = duration.minutes();
        const seconds = duration.seconds();

        countdownElement.innerHTML = `抢票已结束，下次再见！</br> ${days}天${hours.toString().padStart(2, '0')}小时${minutes.toString().padStart(2, '0')}分钟${seconds.toString().padStart(2, '0')}秒`;
        buyTicketButton.style.backgroundColor = "gray";
        buyTicketButton.disabled = true;
        displayed = false;
    }

}

updateCountdown();
setInterval(updateCountdown, 1000);

buyTicketButton.addEventListener("click", function () {
    if (buyTicketButton.disabled) {
        return;
    }
    document.getElementById("form-modal").style.display = "flex";
});

document.getElementById("form-modal").addEventListener("click", function (event) {
    if (event.target === event.currentTarget) {
        event.target.style.display = "none";
    }
});
</script>
    <script>
    function resize_bg() {
        console.log("start");        
        var v = document.getElementById("background_video")
        var mh = window.innerHeight
        var mw = window.innerWidth
        v.style.height = ''
        v.style.width = ''
        var vh = parseInt(window.getComputedStyle(v).height.replace('px', ''))
        var vw = parseInt(window.getComputedStyle(v).width.replace('px', ''))
        
        if ((mh/mw-vh/vw)>0){
            v.style.height = mh.toString()+'px'
            v.style.widows = (vw*vh/mh).toString()+'px'
        }else{
            v.style.widows = mw.toString()+'px'
            v.style.height = (vh*vw/mw).toString()+'px'
        }
        // change background_video::before size
        var b = document.getElementById("background_video::before")
        b.style.height = v.style.height
        b.style.width = v.style.width
    }
    window.onload = resize_bg
    window.addEventListener('resize',resize_bg)
    </script>
</body>

</html>